package com.weixing.mall.provider.controller.sys;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.AccessLog;
import com.weixing.mall.provider.model.domain.LoginLog;
import com.weixing.mall.provider.model.query.SysAccessLogQuery;
import com.weixing.mall.provider.model.query.SysLoginLogQuery;
import com.weixing.mall.provider.model.vo.sys.SysAccessLogVo;
import com.weixing.mall.provider.service.IAccessLogService;
import com.weixing.mall.provider.service.ILoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 系统日志接口
 *
 * @author siashan
 * @since 1.0
 */
@RestController
@RequestMapping("log")
public class SysLogController extends BaseController {

    @Autowired
    private ILoginLogService sysLoginLogService;
    @Autowired
    private IAccessLogService sysAccessLogService;

    /**
     * 查看登录日志
     * @param query 查询条件
     * @return 日志列表
     */
    @GetMapping("/loginLog")
    public Result<IPage<LoginLog>> loginLoglist(SysLoginLogQuery query) {
        return ResultUtil.success(sysLoginLogService.selectAllOfPage(initMpPage(), query));
    }


    /**
     * 查看访问日志
     * @param query 查询条件
     * @return 访问日志
     */
    @GetMapping("/accesslog")
    public Result accessloglist(SysAccessLogQuery query) {
        return ResultUtil.success(sysAccessLogService.selectOfPage(initMpPage(), query));
    }

    /**
     * 访问日志导出
     *
     * @param query     查询条件
     * @param response  HttpServletResponse
     */
    @SysLog(type = OperateEnum.D, desp = "访问日志导出")
    @GetMapping("accesslog/import")
    public void accesslogImport(SysAccessLogQuery query, HttpServletResponse response) {
        List<SysAccessLogVo> records = sysAccessLogService.selectLogVoAll(query);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        try {
            String fileName = URLEncoder.encode("访问日志", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SysAccessLogVo.class).sheet("sheet1").doWrite(records);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除访问日志
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/accesslogBatchDel")
    @SysLog(type = OperateEnum.D, desp = "批量删除访问日志")
    public Result accesslogBatchDel(SysAccessLogQuery query) {
        sysAccessLogService.batchDelete(query);
        return ResultUtil.success();
    }

    /**
     * 批量删除登录日志
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/loginlogBatchDel")
    @SysLog(type = OperateEnum.D, desp = "批量删除访问日志")
    public Result loginlogBatchDel(SysLoginLogQuery query) {
        sysLoginLogService.batchDelete(query);
        return ResultUtil.success();
    }


    /**
     * 删除登录日志
     * @param id 日志id
     * @return 结果
     */
    @SysLog(type = OperateEnum.D, desp = "删除登录日志")
    @PostMapping("delLogin/{id}")
    public Result delLogin(@PathVariable long id) {
        return sysLoginLogService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

    /**
     * 删除访问日志
     * @param id 日志id
     * @return 结果
     */
    @SysLog(type = OperateEnum.D, desp = "删除访问日志")
    @PostMapping("delAccess/{id}")
    public Result delAccess(@PathVariable long id) {
        return sysAccessLogService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

}
